package com.tentact.servlets;


import com.tentact.service.AdminService;
import com.tentact.vo.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AdminServlet", value = "/AdminServlet")
public class AdminServlet extends HttpServlet {
    AdminService service = new AdminService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String userpwd = request.getParameter("userpwd");

        Employee employee = new Employee(username, userpwd);
        Employee e = service.selectByEmployees(employee);

        if (e != null) {
            int employeeid = e.getEmployeeid();
            //获取会话
            HttpSession session = request.getSession();
            //将用户名和密码存入会话属性里
            session.setAttribute("username", username);
            session.setAttribute("userpwd", userpwd);
            session.setAttribute("employeeid",employeeid);

            request.getRequestDispatcher("main.jsp").forward(request, response);


        } else {
            String msg = "用户名或密码错误";
            request.setAttribute("msg", msg);


            request.getRequestDispatcher("login.jsp").forward(request, response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }
}
